package com.dousb.cloudlibrary.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dousb.cloudlibrary.common.ErrorCode;
import com.dousb.cloudlibrary.constant.CommonConstant;
import com.dousb.cloudlibrary.exception.ThrowUtils;
import com.dousb.cloudlibrary.mapper.RecordMapper;
import com.dousb.cloudlibrary.model.dto.record.RecordQueryRequest;
import com.dousb.cloudlibrary.model.entity.Record;
import com.dousb.cloudlibrary.model.vo.RecordVO;
import com.dousb.cloudlibrary.service.RecordService;
import com.dousb.cloudlibrary.service.UserService;
import com.dousb.cloudlibrary.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 借阅表服务实现
 */
@Service
@Slf4j
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements RecordService {

    @Resource
    private UserService userService;

    /**
     * 校验数据
     *
     * @param record
     * @param add    对创建的数据进行校验
     */
    @Override
    public void validRecord(Record record, boolean add) {
        ThrowUtils.throwIf(record == null, ErrorCode.PARAMS_ERROR);
        String record_name = record.getRecord_bookname();
        String record_bookisbn = record.getRecord_bookisbn();
        // 创建数据时，参数不能为空
        if (add) {
            ThrowUtils.throwIf(StringUtils.isBlank(record_name), ErrorCode.PARAMS_ERROR);
            ThrowUtils.throwIf(StringUtils.isBlank(record_bookisbn), ErrorCode.PARAMS_ERROR);

        }
        // 修改数据时，有参数则校验
        if (StringUtils.isNotBlank(record_name)) {
            ThrowUtils.throwIf(record_name.length() > 80, ErrorCode.PARAMS_ERROR, "书名过长");
        }
        if (StringUtils.isNotBlank(record_bookisbn)) {
            ThrowUtils.throwIf(record_bookisbn.length() > 32, ErrorCode.PARAMS_ERROR, "ISBN过长");
        }
    }

    /**
     * 获取查询条件
     *
     * @param recordQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<Record> getQueryWrapper(RecordQueryRequest recordQueryRequest) {
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        if (recordQueryRequest == null) {
            return queryWrapper;
        }
        Long record_id = recordQueryRequest.getRecord_id();
        String record_bookname = recordQueryRequest.getRecord_bookname();
        String record_bookisbn = recordQueryRequest.getRecord_bookisbn();
        Long record_borrower = recordQueryRequest.getRecord_borrower();
        Integer record_return = recordQueryRequest.getRecord_return();
        String sortField = recordQueryRequest.getSortField();
        String sortOrder = recordQueryRequest.getSortOrder();
        // 模糊查询
        queryWrapper.like(StringUtils.isNotBlank(record_bookname), "record_bookname", record_bookname);
        queryWrapper.like(StringUtils.isNotBlank(record_bookisbn), "record_bookisbn", record_bookisbn);
        // 精确查询
        queryWrapper.eq(ObjectUtils.isNotEmpty(record_id), "record_id", record_id);
        queryWrapper.eq(ObjectUtils.isNotEmpty(record_borrower), "record_borrower", record_borrower);
        queryWrapper.eq(ObjectUtils.isNotEmpty(record_return), "record_return", record_return);

        // 排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    /**
     * 获取借阅表封装
     *
     * @param record
     * @param request
     * @return
     */
    @Override
    public RecordVO getRecordVO(Record record, HttpServletRequest request) {
        if (record == null) {
            return null;
        }
        RecordVO recordVO = new RecordVO();
        recordVO.setRecord_id(record.getRecord_id());
        recordVO.setRecord_bookname(record.getRecord_bookname());
        recordVO.setRecord_bookisbn(record.getRecord_bookisbn());
        recordVO.setRecord_borrower(record.getRecord_borrower());
        recordVO.setRecord_borrowtime(record.getRecord_borrowtime());
        recordVO.setRecord_remandtime(record.getRecord_remandtime());
        return recordVO;
    }

    /**
     * 分页获取借阅表封装
     *
     * @param recordPage
     * @param request
     * @return
     */
    @Override
    public Page<RecordVO> getRecordVOPage(Page<Record> recordPage, HttpServletRequest request) {
        List<Record> recordList = recordPage.getRecords();
        List<RecordVO> recordVOList = recordList.stream().map(record -> {
            RecordVO recordVO = new RecordVO();
            BeanUtils.copyProperties(record, recordVO);
            return recordVO;
        }).collect(Collectors.toList());

        // 构造 RecordVO 的分页对象
        Page<RecordVO> recordVOPage = new Page<>(
                recordPage.getCurrent(),
                recordPage.getPages(),
                recordPage.getTotal()
        );
        recordVOPage.setRecords(recordVOList);
        return recordVOPage;
    }
}
